//
//  NewsDetailVC.m
//  Brand
//
//  Created by Faker on 4/23/14.
//  Copyright (c) 2014 Faker. All rights reserved.
//

#import "NewsDetailVC.h"
#import "CollectionFixedHeaderLayout.h"

#import "Header.h"

#define DefautHeight    (isiOS6? kScreen_Height-self.navTopContentView.bottom - 20 : kScreen_Height-self.navTopContentView.bottom)

#define CellIdentifer @"CellIdentifer"

@interface NewsDetailVC ()

@property (weak, nonatomic) IBOutlet UIView *navTopContentView;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property (strong, nonatomic) UICollectionView* myCollectionView;

@end

@implementation NewsDetailVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.titleLabel.text = self.title;
    [self drawNavTopContentView:self.navTopContentView];
     [self setUp];
    
}

-(void)setUp
{
    
    CGRect frame = CGRectMake(0, self.navTopContentView.bottom,320,DefautHeight);
    self.myCollectionView = [self drawCollectionViewWithFrame:frame andFlowLayout:[self getFlowLayout]];
    [self registerCellAndHeaderForCollectionView:self.myCollectionView];
    self.myCollectionView.contentOffsetLeft = self.view.width*self.currentItemIndex;
   
    [self.view addSubview:self.myCollectionView];
    
}


#pragma mark - CollectionView methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
   
    return self.dataArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier =CellIdentifer;
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
   
    NSDictionary* dic = [self.dataArray objectAtIndex:indexPath.row];
 
    UIWebView* webView = (UIWebView*)[cell viewWithTag:500];
    webView.scrollView.alpha = 0;
    webView.delegate = self;
    NSLog(@"url = %@",[dic objectForKey:@"url"]);
    NSURL* url = [NSURL URLWithString:[dic objectForKey:@"url"]];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

#pragma mark - webviewdelegate methods
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [kApp displayWaitViewWithAllMaskView:NO];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [kApp hideWaitView];
    [self performSelector:@selector(delayWebViewShow:) withObject:webView afterDelay:0.3];
}

-(void)delayWebViewShow:(UIWebView*)webView
{
    [UIView animateWithDuration:0.1 animations:^{
        webView.scrollView.alpha = 1.0;
    }];
}


-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [kApp hideWaitView];
    [PopTips tips:self.view Msg:NotifyNoNet];
}



#pragma mark - UIScrollView methods
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    float offX = scrollView.contentOffsetLeft;
    int index = floorf(offX/self.view.width);
    
    if(index<0)
    {
        index = 0;
    }
    if(index>=self.dataArray.count)
    {
        index = self.dataArray.count - 1;
    }
       self.currentItemIndex = index;
    
}

#pragma mark - 生成collectionview和布局布局管理器
-(UICollectionView*)drawCollectionViewWithFrame:(CGRect)frame andFlowLayout:(CollectionFixedHeaderLayout*)flowLayout
{
    UICollectionView* myCollectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:flowLayout];
    myCollectionView.showsVerticalScrollIndicator = NO;
    myCollectionView.showsHorizontalScrollIndicator = NO;
    myCollectionView.dataSource = self;
    myCollectionView.delegate = self;
    myCollectionView.pagingEnabled = YES;
    myCollectionView.backgroundColor = [UIColor clearColor];
    return myCollectionView;
}

-(void)registerCellAndHeaderForCollectionView:(UICollectionView*)collectionView {
    UINib *cellNib = nil;
    cellNib = [UINib nibWithNibName:@"NewsCollectionCell" bundle:nil];
    [collectionView registerNib:cellNib forCellWithReuseIdentifier:CellIdentifer];
}

//一个布局管理器只能对应一个collectionView
-(CollectionFixedHeaderLayout*)getFlowLayout
{
    CollectionFixedHeaderLayout *flowLayout = [[CollectionFixedHeaderLayout alloc] init];
    flowLayout.isFiexed = NO;
    
    [flowLayout setItemSize:CGSizeMake(320, DefautHeight)];
    flowLayout.minimumInteritemSpacing =0;
    flowLayout.minimumLineSpacing = 0;
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    return flowLayout;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)goBack:(id)sender {
    [kApp hideWaitView];
    [[AFGetData sharedInstance] removeAllGetAndPostRequest];
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)goToShare:(id)sender {
    
    [kApp initShareViewWithUrl:ShareUrl andWithTitle:ShareTitle andWithInfo:NotifyRecommendShare andWithImageUlr:ShareImageUrl andIsSuccess:^(BOOL isSuccess) {
        if(isSuccess)
        {
            [PopTips tips:kApp.rootNav.view Msg:NotifyShareSuccess];
        }
        else{
            [PopTips tips:kApp.rootNav.view Msg:NotifyShareError];
        }
    }];
}

@end
